﻿Ext.define('app.prm.detail.DiscussDetail', {
    extend: 'Ext.panel.Panel',
    border: true,
    collapsed: true,
    collapsible: true,
    autoScroll: true,
    bodyStyle: 'padding:5px;',
    style: 'margin:3px',
    htmlContent: '',
    topicId: 0,
    initComponent: function () {
        var me = this;
        me.items = [
            {
                border: false,
                html: '<b>' + me.htmlContent + '</b>'
            },
            me.getReplyPanel(),
            me.getReplyTextarea()
        ]

        me.bbar = [
            "->",
            {
                text: '提交', handler: function () {
                    var content = me.getReplyTextarea().getValue();
                    if (content == "") {
                        Ext.Msg.alert('系统提示', '请输入内容');
                        return false;
                    }
                    App.AjaxPost('/api/services/app/topic/CreateTopicReply', { TopicId: me.topicId, Content: content }, function () {
                        me.getReplyTextarea().reset();
                        me.initReply(me);
                    });
                }
            },
            { text: '重置', handler: function () { me.getReplyTextarea().reset(); } }
        ];
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            me.initReply(me);
        }
    },
    getReplyTextarea: function () {
        if (!this.replyTextarea) {
            this.replyTextarea = new Ext.form.field.TextArea({
                emptyText: '回答',
                height: 150,
                width: '100%'
            });
        }
        return this.replyTextarea;
    },
    initReply: function (me) {
        App.AjaxGet('/api/services/app/topic/GetTopicReply?topicId=' + me.topicId, function (data) {
            me.getReplyPanel().removeAll(true);
            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];

                var panel = Ext.create('Ext.panel.Panel', {
                    style: 'margin:10px 0 10px 0',
                    border: false,
                    items: [
                        { border: false, style: 'color:blue', html: '<b>---' + record.createBy + '.' + record.createOn + '</b>' },
                        { border: false, style: 'margin:0 0 0 10px', html: record.content }
                    ]
                });
                me.getReplyPanel().add(panel);
            }
        });
    },
    getReplyPanel: function () {
        if (!this.replyPanel) {
            this.replyPanel = new Ext.panel.Panel({
                border: false,
                items: []
            });
        }
        return this.replyPanel;
    }
})